package com.autonavi.yunda.yunji.core.model.dto;

import com.autonavi.yunda.yunji.core.enums.RoleType;
import com.autonavi.yunda.yunji.core.enums.UserPermission;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cizhong.wcs
 * @date 2023/9/4 14:06
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "yunji_user", autoResultMap = true)
public class UserDTO {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String userId;
    private String nickName;
    private String lastName;
    private RoleType userRole;
    @TableField(value = "permissions", typeHandler = JacksonTypeHandler.class)
    private List<String> permissions;
    @TableField(exist = false)
    private List<String> projectIds;

    public List<UserPermission> getPermissions() {
        if (CollectionUtils.isEmpty(permissions)) {
            return new ArrayList<>();
        }
        return permissions.stream().map(UserPermission::valueOf).collect(Collectors.toList());
    }
}
